# _*_ coding: utf-8 _*_
from Data.Table import Table,Data,Field,tableStruct
import json
import re
import Engine.Engine as Engine
from Engine.MatchModule import *

'''
主要负责数据的显示以及主函数
'''
class Service:
    #con = Con.Container()
    engine = Engine.Engine()
    sql=None
    def __init__(self):
        self.sql=None
    def get_alter_table_name(self, sql):
        try:
            table_name = re.match(pattern_alter, sql, re.I).group(1)
            return table_name
        except:
            print "Input instruction error, please reinput!"
    def get_alter_column(self, sql):
        try:
            column_info = re.match(pattern_alter, sql, re.I).group(2)
            return column_info
        except:
            print "Input instruction error, please reinput!"
    def get_add_alter_field(self, sql):
        column = re.match(alter_add,self.get_alter_column(sql), re.I).group(1)
        print column.split()[0]
        return column.split()[0]
    def get_add_alter_type(self, sql):
        column = re.match(alter_add, self.get_alter_column(sql), re.I).group(1)
        print column.split()[1]
        return column.split()[1]
    def get_add_alter_bind(self,sql):
        column = re.match(alter_add, self.get_alter_column(sql), re.I).group(1)
        Binds_ = {}
        if re.search("not null", column, re.I):
            Binds_[column.split()[0]]=re.search("not null", column, re.I).group()
        elif re.search("null", column, re.I):
            Binds_[column.split()[0]]=re.search("null", column, re.I).group()
        return Binds_
    def get_drop_column(self, sql):
        column = re.match(alter_drop, self.get_alter_column(sql), re.I).group(1)
        name = column.split()[0]
        return name
    def add_column(self, sql):
        pass
    def is_drop(self, sql):
        column = self.get_alter_column(sql)
        if re.search("DROP", column, re.I):
            return True
        else:
            return False
    def is_add(self, sql):
        try:
            if "add" in sql or "ADD" in sql:
                return True
            else:
                return False
        except:
            print "Input error!"
    def is_desc(self, sql):
        if re.match(desc, sql, re.I) != None:
            return True
        else:
            return False
    def get_desc_name(self, sql):
        name = re.match(desc, sql, re.I).group(1)
        return name
if __name__ == '__main__':
    sql = raw_input("sql>:")
    s = Service()
    #print s.sql_parse()
    #s.get_alter_table_name(sql)
    #print s.get_alter_column(sql)
    #s.get_add_alter_field(sql)
    #s.get_add_alter_type(sql)
    #print s.get_add_alter_bind(sql)
    #s.get_drop_column(sql)
    #print s.is_add(sql)
    ##print s.is_drop(sql)
    print s.is_desc(sql)
    '''
    Service负责调用各种函数，产生数据用于输出
    '''